package com.study.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.study.json.entity.Feature;

import java.io.File;
import java.io.IOException;

public class Jackson {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Feature feature = mapper.readValue(new File("src/main/resources/feature.json"), Feature.class);
        String featureString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(feature);
        System.out.println(featureString);

        JsonNode users = mapper.readTree(new File("src/main/resources/users.json"));
        for(JsonNode node : users){
            int id = node.path("id").asInt();
            JsonNode nodeName = node.path("name");
            if(!nodeName.isMissingNode()){
                String firstName = nodeName.path("first").asText();
                String lastName = nodeName.path("last").asText();
                System.out.println(id + "," + firstName + "," + lastName);
            }
            JsonNode nodeContact = node.path("contact");
            if(nodeContact.isArray()){
                for(JsonNode node1 : nodeContact) {
                    String type = node1.path("type").asText();
                    String ref = node1.path("ref").asText();
                    System.out.println(type + ", " + ref);
                }
            }
        }

    }
}
